/**
 * Description: 公共方法类
 * Author: longyihuang
 * Date: 2022/3/23
 * Location: shenzhen
 * E-Mail: 475865662@qq.com
 */

const SECOND = 1000;
const MINUTE = SECOND * 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const YEAR = DAY * 365;
/**
 * 判断字符中是否含有集合中的字符
 */
export const includeInList = (str, list) => {
  let res = false
  if (list && str && str != '') {
    for (let i = 0; i < list.length; i++) {
      const item = list[i]
      if (typeof item === 'string' && str.indexOf(item) > -1) {
        res = true
        break;
      }
    }
  }
  return res
}
/**
 * html长度档位格式化
 */
export const lengthUnit = function(length) {
  if (length === undefined || length === null) {
    return null
  }
  if (typeof length === "number") {
    length += ''
  }
  if (includeInList(length, ['px','em','pt','ex','pc','in','mm','cm', '%'])) {
    return length
  }
  return length + 'px'
}
/**
 * 获取guid
 */
export function guid () {
  let s4 = ((1 + Math.random()) * 0x10000 || 0).toString(16).substring(1)
  let s5 = s4 + Date.now() + Math.floor(Math.random() * 999999)
  return s5.replace(new RegExp('\\.', 'gm'), '')
}




/**
 * 时间戳转日期时间字符
 * @param value 时间戳
 * @param formatStr  格式
 * @returns {null}
 */
export const convertTimestamp2DateTimeString = function(timestamp, format='YDHMS') {
  let result = ''
  if (timestamp && timestamp >= 0) {
    if (format.toUpperCase().includes('Y')) {
      let year = Math.floor(timestamp / YEAR)
      if (year) {
        timestamp -= year * YEAR
        result += `${year}年`
      }
    }
    if (format.toUpperCase().includes('D')) {
      let day = Math.floor(timestamp / DAY)
      if (day) {
        timestamp -= day * DAY
        result += `${day}天`
      }
    }
    if (format.toUpperCase().includes('H')) {
      let hour = Math.floor(timestamp / HOUR)
      if (hour) {
        timestamp -= hour * HOUR
        result += `${hour}小时`
      }
    }
    if (format.toUpperCase().includes('M')) {
      let minute = Math.floor(timestamp / MINUTE)
      if (minute) {
        timestamp -= minute * MINUTE
        result += `${minute}分`
      }
    }
    if (format.toUpperCase().includes('S')) {
      let second = parseInt(timestamp / SECOND)
      if (second) {
        result += `${second}秒`
      }
    }
  }
  return result
}
